// RepwdPresenter.java
package com.hwpt.hwaccount.feature.auth;

import android.util.Log;

public class RepwdPresenter implements RepwdContract.Presenter {

    private RepwdContract.View view;
    private RepwdContract.Model model;
    private String verifiedPhone; // 保存验证通过的手机号

    public RepwdPresenter(RepwdContract.View view, RepwdContract.Model model) {
        this.view = view;
        this.model = model;
    }

    @Override
    public void sendVerificationCode(String phone) {
        Log.d("RepwdPresenter", "请求发送验证码: " + phone);
        model.sendVerificationCode(phone, new RepwdContract.OnVerificationCodeSentListener() {
            @Override
            public void onSuccess() {
                Log.d("RepwdPresenter", "验证码发送成功回调");
                view.showToast("验证码发送成功");
                view.startCountDownTimer();
            }

            @Override
            public void onFailure(String errorMessage) {
                Log.e("RepwdPresenter", "验证码发送失败: " + errorMessage);
                view.showToast(errorMessage);
            }
        });
    }

    @Override
    public void verifyCode(String username, String code) {
        model.verifyCode(username, code, new RepwdContract.OnCodeVerifiedListener() {
            @Override
            public void onSuccess() {
                verifiedPhone = username; // 保存验证通过的手机号
                view.showToast("验证成功");
                view.switchToResetPassword();
            }

            @Override
            public void onFailure(String errorMessage) {
                view.showToast(errorMessage);
            }
        });
    }

    @Override
    public void resetPassword(String newPassword) {
        if (verifiedPhone == null) {
            view.showToast("请先完成手机验证");
            return;
        }

        model.resetPassword(verifiedPhone, newPassword, new RepwdContract.OnPasswordResetListener() {
            @Override
            public void onSuccess() {
                view.showToast("密码重置成功");
                view.finishActivity();
            }

            @Override
            public void onFailure(String errorMessage) {
                view.showToast(errorMessage);
            }
        });
    }

    @Override
    public void onDestroy() {
        view = null;
    }
}
